//
//  MenuTopView.m
//  AllYoga
//
//  Created by liwei on 2017/5/31.
//  Copyright © 2017年 biyunkeji. All rights reserved.
//

#import "MenuTopView.h"
#import "SwipeView.h"
#import "YGTagView.h"

#define YGRGB(r,g,b,a) [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:a]
//屏幕宽度比例
#define kXX [UIScreen mainScreen].bounds.size.width / 375
//屏幕高度比例
#define kYY [UIScreen mainScreen].bounds.size.height / 667
#define YGWeak __weak typeof(self) weakSelf = self


@interface MenuTopView ()<SwipeViewDelegate,SwipeViewDataSource>

//滚动视图
@property (nonatomic,strong) SwipeView *swipeView;

//左边视图
@property (nonatomic,strong) UIView *menuView;

//左边菜单栏按钮
@property (nonatomic,strong) UIButton *menuBtn;

//记录当前居中展示的cell
@property (nonatomic,assign) NSInteger currentIndex;

//标识是否是滚动造成的
@property (nonatomic,assign) BOOL scrollSwipeView;

@end

@implementation MenuTopView

- (UIView *)menuView {

    if (!_menuView) {
        _menuView = [[UIView alloc] init];
        _menuView.layer.shadowColor = YGRGB(213, 204, 204, 0.8).CGColor;
        _menuView.layer.shadowOffset = CGSizeMake(4, 0);
        _menuView.layer.shadowOpacity = 0.8;
        _menuView.layer.backgroundColor = [UIColor whiteColor].CGColor;
        [self addSubview:_menuView];
    }
    return _menuView;
}


- (UIButton *)menuBtn {

    if (!_menuBtn) {
        _menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_menuBtn setImage:[UIImage imageNamed:@"recommend_menu"] forState:UIControlStateNormal];
        [_menuBtn addTarget:self action:@selector(didClickMenuBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.menuView addSubview:_menuBtn];
    }
    return _menuBtn;
}



- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        [self loadSubViews];
        self.layer.shadowOpacity = 0.6;
        self.layer.shadowOffset = CGSizeMake(0, 4);
        self.layer.shadowColor = YGRGB(213, 204, 204, 0.6).CGColor;
        self.layer.backgroundColor = [UIColor clearColor].CGColor;
        
        self.normalFont = [UIFont systemFontOfSize:12];
        self.selectedFont = [UIFont systemFontOfSize:15];
        self.normalColor = [UIColor lightGrayColor];
        self.selectedColor = [UIColor darkGrayColor];
        
        [self updateSubViewsFrame];
        
    }
    return self;
}

- (void)loadSubViews {
    
    self.swipeView = [[SwipeView alloc] initWithFrame:self.bounds];
    self.swipeView.backgroundColor = [UIColor whiteColor];
    self.swipeView.alignment = SwipeViewAlignmentCenter;
    self.swipeView.pagingEnabled = YES;
    self.swipeView.itemsPerPage = 1;
    self.swipeView.truncateFinalPage = YES;
    self.swipeView.dataSource = self;
    self.swipeView.delegate = self;
    [self addSubview:self.swipeView];
    self.currentIndex = 0;
}


- (void)updateSubViewsFrame {

    self.menuView.frame = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
    
    self.menuBtn.center = CGPointMake(self.menuView.frame.size.width * 0.5, self.menuView.frame.size.height * 0.5);
    self.menuBtn.bounds = CGRectMake(0, 0, 20 * kYY, 20 * kYY);
    
    [self bringSubviewToFront:self.menuView];
}

- (void)setDataSource:(NSMutableArray *)dataSource {

    _dataSource = dataSource;

    self.currentIndex = 0;
    [self.swipeView reloadData];
    
    [self.swipeView scrollToItemAtIndex:self.currentIndex duration:0];
    [self swipeView:self.swipeView didSelectItemAtIndex:self.currentIndex];

}

#pragma mark - SwipeViewDelegate SwipeViewDataSource
- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView {
    
    return self.dataSource.count;
}

- (CGSize)swipeViewItemSize:(SwipeView *)swipeView {
    
    return CGSizeMake(self.frame.size.width / 3, self.frame.size.height);
}

- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    if (!view) {
        view = [[NSBundle mainBundle] loadNibNamed:@"YGTagView" owner:self options:nil][0];
    }
    ((YGTagView *)view).tagTitle = self.dataSource[index];
    
    ((YGTagView *)view).tagSelected = (self.currentIndex == index);
    return view;
}

- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index {
    
    self.scrollSwipeView = NO;
    
    [self updateScrollOffsetWith:index];
    [self triggerTopItemRequestWith:index];
    
}

- (void)swipeViewWillBeginDragging:(SwipeView *)swipeView {
    
    self.scrollSwipeView = YES;
}

- (void)swipeViewDidEndDecelerating:(SwipeView *)swipeView {
    if (!self.scrollSwipeView) {
        return;
    }
    [self updateScrollOffsetWith:swipeView.currentItemIndex];
    [self triggerTopItemRequestWith:swipeView.currentItemIndex];
}


//点击菜单栏按钮
- (void)didClickMenuBtn {
    
    if (self.clickMenuColumn) {
        self.clickMenuColumn();
    }
}

- (void)updateScrollOffsetWith:(NSInteger)itemIndex {
    
    self.scrollSwipeView = NO;
    
    if (self.currentIndex == itemIndex) {
        return;
    }
    YGTagView *oldView = (YGTagView *)[self.swipeView itemViewAtIndex:self.currentIndex];
    oldView.tagSelected = NO;
    
    YGTagView *currentView = (YGTagView *)[self.swipeView itemViewAtIndex:itemIndex];
    currentView.tagSelected = YES;
    self.currentIndex = itemIndex;
    [self.swipeView scrollToItemAtIndex:itemIndex duration:0.2];
}

- (void)triggerTopItemRequestWith:(int)itemIndex {
    
    if (self.didClickMenuTop) {
        self.didClickMenuTop(itemIndex);
    }
}

- (void)dealloc {
    
    self.swipeView.delegate = nil;
    self.swipeView.dataSource = nil;
}

@end
